"use strict";

import Vue from 'vue';
import axios from "axios";


let config = {
    baseURL: 'https://zhcg.lx.gov.cn:7443/api/zlb/',
    timeout: 60 * 5000, //响应过期时间
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    // withCredentials: true, // Check cross-site Access-Control 时候允许跨域
};

const _axios = axios.create(config);

let config_json = {
    baseURL: 'https://zhcg.lx.gov.cn:7443/api/zlb/',
    timeout: 60 * 5000, //响应过期时间
    headers: {'Content-Type': 'application/json'},
    // withCredentials: true, // Check cross-site Access-Control 时候允许跨域
};

const _axiosJson = axios.create(config_json);

let config_lbs = {
    baseURL: '/geosearch/v3',
    timeout: 60 * 5000, //响应过期时间
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    // withCredentials: true, // Check cross-site Access-Control 时候允许跨域
};
const _axiosLbs = axios.create(config_lbs);
let config_jgfb = {
    baseURL: '/sspwebservice/services',
    timeout: 60 * 5000, //响应过期时间
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    // withCredentials: true, // Check cross-site Access-Control 时候允许跨域
};
const _axiosJgfb = axios.create(config_jgfb);

const _axiosfile = axios.create({
    timeout: 60 * 5000, //响应过期时间
    headers: {'Content-Type': 'multipart/form-data'},
    mediaURL: 'https://zhcg.lx.gov.cn:7443/file/file/'
});
var __makeurl = function (url) {
    var turl = url
    if (url.indexOf('http://') < 0 &&
        url.indexOf('https://') < 0) {
        var root = _axiosfile.defaults.mediaURL
        if (root) {
            var strend = root.substring(root.length - 1)
            if (url.substring(0, 1) === strend && strend === '/') {
                turl = url.substring(1)
            }
            turl = root + turl
        }
    }
    return turl
}
_axiosfile.postFile=function(url, file, fileName,extra){
    var httpUrl=__makeurl(url)
    let forms = new FormData()
    if (file instanceof Array){
        for (let i = 0; i < file.length; i++) {
            var d=file[i]
            if (d.name){
                forms.append('file', d,d.name)
            }else{
                forms.append('file', d)
            }
        }
    }else{
        if (fileName==undefined||fileName==null||fileName==''){
            fileName=file.name
        }
        if (fileName!=undefined&&fileName!=''){
            forms.append('file', file, fileName)
        }else{
            forms.append('file', file)
        }
    }

    return new Promise((resolve, reject) => {
        _axiosfile.post(httpUrl, forms,{ params:extra})
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error)
            })
    })
}
_axiosfile.postFileExt=function(p){
    var fun=(p instanceof Array)?'/uploadFiles':'/uploadFile'
    var param={}
    if (window._photoParams)
        param=window._photoParams()
    return _axiosfile.postFile(fun,p,null,param)
}
_axios.interceptors.request.use(
    function (config) {
        //发送之前操作
        return config;
    },
    function (error) {
        //错误之后提示
        this.Toast('error', error.data.msg);
        return Promise.reject(error);
    }
);

// 添加一个响应拦截器
_axios.interceptors.response.use(
    function (response) {
        // 对响应数据做些什么
        return response;
    },
    function (error) {
        // 处理响应错误
        return Promise.reject(error);
    }
);

Plugin.install = function (Vue, options) {
    Vue.axios = _axios;
    window.axios = _axios;
    Vue.axiosfile = _axiosfile;
    window.axiosfile = _axiosfile;
    Vue.axiosJson = _axiosJson;
    window.axiosJson = _axiosJson;

    window.axiosLbs=_axiosLbs
    window.axiosJgfb=_axiosJgfb

    Object.defineProperties(Vue.prototype, {
        axios: {
            get() {
                return _axios;
            }
        },
        $axios: {
            get() {
                return _axios;
            }
        },
        axiosJson: {
            get() {
                return _axiosJson;
            }
        },
        $axiosJson: {
            get() {
                return _axiosJson;
            }
        },
        axiosfile: {
            get() {
                return _axiosfile;
            }
        },
        $axiosfile: {
            get() {
                return _axiosfile;
            }
        },
    });
};

Vue.use(Plugin)

export default Plugin;//_axios;
